package com.cjh.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjh.common.dto.EvaluatePagingDTO;
import com.cjh.common.dto.PagingDTO;
import com.cjh.common.entity.Evaluate;
import com.cjh.common.entity.OrderStatus;
import com.cjh.common.entity.StoreOrder;
import com.cjh.common.entity.User;
import com.cjh.common.mapper.OrderStatusMapper;
import com.cjh.common.mapper.StoreOrderMapper;
import com.cjh.common.mapper.UserMapper;
import com.cjh.common.response.Result;
import com.cjh.common.service.EvaluateService;
import com.cjh.common.mapper.EvaluateMapper;
import com.cjh.common.service.PermissionService;
import com.cjh.common.wxDto.AddEvaluateDTO;
import com.cjh.common.wxDto.EvaluateDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class EvaluateServiceImpl extends ServiceImpl<EvaluateMapper, Evaluate>
    implements EvaluateService{
    @Autowired
    EvaluateMapper evaluateMapper;
    @Autowired
    StoreOrderMapper storeOrderMapper;
    @Autowired
    OrderStatusMapper orderStatusMapper;
    @Autowired
    PermissionService permissionService;
    @Autowired
    UserMapper userMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result addEvaluate(AddEvaluateDTO addEvaluateDTO) {
        Evaluate evaluate = new Evaluate();
        BeanUtils.copyProperties(addEvaluateDTO,evaluate);
        evaluate.setCreateTime(new Date());
        evaluateMapper.insert(evaluate);

        StoreOrder storeOrder = new StoreOrder();
        storeOrder.setOrderId(addEvaluateDTO.getOrderId());
        storeOrder.setEvaluateId(evaluate.getEvaluateId());
        storeOrderMapper.updateById(storeOrder);

        OrderStatus orderStatus = new OrderStatus();
        orderStatus.setOrderId(addEvaluateDTO.getOrderId());
        orderStatus.setChangeType(6);
        orderStatus.setChangeMessage("用户评价");
        orderStatus.setChangeTime(new Date());
        orderStatusMapper.insert(orderStatus);

        return new Result().setCode(0).setMessage("评价成功！");
    }

    @Override
    public Result getAllEvaluate(Integer id) {
        List<EvaluateDTO> allEvaluateByWxUserId = evaluateMapper.getAllEvaluateByWxUserId(id);
        return new Result().setCode(0).setMessage("获取成功！").setData(allEvaluateByWxUserId);
    }

    @Override
    public Result getPagingStoreEvaluate(EvaluatePagingDTO evaluatePagingDTO1) {
        PagingDTO<com.cjh.common.dto.EvaluateDTO> objectPagingDTO = new PagingDTO<>();
        Integer size = evaluatePagingDTO1.getSize();
        Integer current = evaluatePagingDTO1.getCurrent();
        Integer start = (current-1)*size;
        Integer storeId = evaluatePagingDTO1.getStoreId();
        String username = evaluatePagingDTO1.getUsername();
        Integer userId = permissionService.isSuperAdministrator(username)?null:userMapper.selectOne(new QueryWrapper<User>().eq("username", username)).getUserId();
        List<com.cjh.common.dto.EvaluateDTO> pagingStoreEvaluate = evaluateMapper.getPagingStoreEvaluate(start, size, userId, storeId);
        objectPagingDTO.setSize(size);
        objectPagingDTO.setCurrent(current);
        objectPagingDTO.setTotal(evaluateMapper.getPagingStoreEvaluateTotal(userId, storeId));
        objectPagingDTO.setRecords(pagingStoreEvaluate);
        return new Result().setCode(200).setMessage("操作成功！").setData(objectPagingDTO);
    }
}




